package muview;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class DialogExcelPath extends Dialog {

	private Shell shell = null;
	private GridLayout gridLayout = null;
	private GridData gridData = null;
	private String path = "";
	private Label lab_databasepath;
	private Text txt_databasepath;
	private Button but_databasepath;
	private Button but_confirm;

	DialogExcelPath(Shell parent) {
		super(parent);
	}

	public String open() {
		Shell parent = getParent();
		// display = parent.getDefault();
		shell = new Shell(parent, SWT.TITLE | SWT.CLOSE | SWT.BORDER);
		shell.setText("Excel路徑");
		shell.setBounds(200, 200, 450, 65);
		gridLayout = new GridLayout();
		gridLayout.numColumns = 4;
		shell.setLayout(gridLayout);

		lab_databasepath = new Label(shell, SWT.None);
		lab_databasepath.setText("Excel路徑 :");
		lab_databasepath.setLayoutData(gridData);

		GridData txt_databasepathGridData = new GridData();
		txt_databasepathGridData.widthHint = 265;
		txt_databasepath = new Text(shell, SWT.BORDER);
		txt_databasepath.setLayoutData(txt_databasepathGridData);

		but_databasepath = new Button(shell, SWT.PUSH);
		but_databasepath.setText("選擇");
		but_databasepath.setLayoutData(gridData);
		but_databasepath.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				FileDialog fileDialog = new FileDialog(shell, SWT.OPEN);
				fileDialog.setText("Excel路徑");
				fileDialog.setFilterPath("C:/");
				String[] filterExt = { "*.xls" };
				fileDialog.setFilterExtensions(filterExt);
				String selected = fileDialog.open();
				if (selected != null)
					txt_databasepath.setText(selected);
			}
		});

		but_confirm = new Button(shell, SWT.PUSH);
		but_confirm.setText("確定");
		but_confirm.setLayoutData(gridData);
		but_confirm.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				path = txt_databasepath.getText();
				shell.close();
			}
		});

		gridData = new GridData(GridData.FILL_HORIZONTAL);
		shell.open();
		Display display = parent.getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}

		return path;
	}
}
